package com.ssy.lingxi.common.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

public class DateUtil {
    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串
     */
    public static String timeStamp2Date(Long seconds, String format) {
        if (seconds == null) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(seconds));
    }

    /**
     * 时间与当前时间差
     */
    public static long[] judgeTime(Long time) {
        long diff = time - System.currentTimeMillis();//这样得到的差值是毫秒级别
        //计算得到时分秒
        long data[] = new long[3];
        data[0] = diff / (1000 * 60 * 60 * 24);
        data[1] = (diff - data[0] * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        data[2] = (diff - data[0] * (1000 * 60 * 60 * 24) - data[1] * (1000 * 60 * 60)) / (1000 * 60);
        return data;
    }

    /**
     * 将localDateTime转成格式化时间字符串
     *
     * @param localDateTime
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String formatLocalDateTimeToStr(LocalDateTime localDateTime) {
        return timeStamp2Date(localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(), null);
    }

    /**
     * 将localDateTime转成时间戳
     *
     * @param localDateTime
     * @return 毫秒
     */
    public static Long convertToTimeMillis(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 将时间戳转成localDateTime
     *
     * @param timeMillis 毫秒
     * @return localDateTime
     */
    public static LocalDateTime convertToLocalDateTime(Long timeMillis) {
        if (timeMillis == null) {
            return null;
        }
        return Instant.ofEpochMilli(timeMillis).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param time1 时间戳参数 1
     * @param time1 时间戳参数 2
     * @return String 返回值为：xx天xx小时xx分xx秒
     */
    public static String getDiffTime(long time1, long time2) {
        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = (diff / (60 * 60 * 1000) - day * 24);
        long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

        String dateTime = "";
        if (day > 0) {
            dateTime += day + "天";
        }
        if (hour > 0) {
            dateTime += hour + "时";
        }
        if (min > 0) {
            dateTime += min + "分";
        }
        if (sec > 0) {
            dateTime += sec + "秒";
        }
        return dateTime;
    }

    /**
     * 获取当前日期
     *
     * @return 20220117
     */
    public static String formatLocalDateTime() {
        Date date = new Date();//此时date为当前的时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMdd");
        return dateFormat.format(date);
    }
}